1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
import productSearchApi from '@/lib/product/api/productSearchApi';
import { create } from 'xmlbuilder';
import { createSlug } from '@/core/utils/slug';
export async function getServerSideProps({ query, res }) {
const baseUrl = process.env.SELF_HOST + '/shop/product/';
const { page } = query;
const limit = 2500;
// ⬇️ Ganti juga ke URLSearchParams
const params = new URLSearchParams({
limit,
page: page.replace('.xml', ''),
source: 'sitemap',
}).toString();
const products = await productSearchApi({ query: params });
const sitemap = create('urlset', { encoding: 'utf-8' }).att(
'xmlns',
'http://www.sitemaps.org/schemas/sitemap/0.9'
);
const date = new Date();
products.response.products.forEach((product) => {
const url = sitemap.ele('url');
url.ele('loc', createSlug(baseUrl, product.name, product.id));
url.ele('lastmod', date.toISOString().slice(0, 10));
url.ele('changefreq', 'daily');
url.ele('priority', '0.8');
});
res.setHeader('Content-Type', 'text/xml');
res.write(sitemap.end());
res.end();
return { props: {} };
}
export default function SitemapProducts() {
return null;
}
|